package com.agile.major.service.impl;

import com.agile.major.domain.BaseMajor;
import com.agile.major.domain.FreshGraduateEmployment;
import com.agile.major.repository.BaseMajorRepository;
import com.agile.major.repository.FreshGraduateEmploymentRepository;
import com.agile.major.service.FreshGraduateEmploymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * Service Implementation for managing FreshGraduateEmployment.
 */
@Service
@Transactional
public class FreshGraduateEmploymentServiceImpl implements FreshGraduateEmploymentService{

    private final Logger log = LoggerFactory.getLogger(FreshGraduateEmploymentServiceImpl.class);

    private final FreshGraduateEmploymentRepository freshGraduateEmploymentRepository;
    private BaseMajorRepository baseMajorRepository;
    public FreshGraduateEmploymentServiceImpl(FreshGraduateEmploymentRepository freshGraduateEmploymentRepository,BaseMajorRepository baseMajorRepository) {
        this.freshGraduateEmploymentRepository = freshGraduateEmploymentRepository;
        this.baseMajorRepository = baseMajorRepository;
    }
    @Autowired
    JdbcTemplate jdbcTemplate;
    /**
     * Save a freshGraduateEmployment.
     *
     * @param freshGraduateEmployment the entity to save
     * @return the persisted entity
     */
    @Override
    public FreshGraduateEmployment save(FreshGraduateEmployment freshGraduateEmployment) {
        log.debug("Request to save FreshGraduateEmployment : {}", freshGraduateEmployment);
        return freshGraduateEmploymentRepository.save(freshGraduateEmployment);
    }

    /**
     *  Get all the freshGraduateEmployments.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<FreshGraduateEmployment> findAll(Pageable pageable) {
        log.debug("Request to get all FreshGraduateEmployments");
        return freshGraduateEmploymentRepository.findAll(pageable);
    }

    /**
     *  Get one freshGraduateEmployment by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public FreshGraduateEmployment findOne(Long id) {
        log.debug("Request to get FreshGraduateEmployment : {}", id);
        return freshGraduateEmploymentRepository.findOne(id);
    }

    /**
     *  Delete the  freshGraduateEmployment by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete FreshGraduateEmployment : {}", id);
        freshGraduateEmploymentRepository.delete(id);
    }

    @Override
    public List<FreshGraduateEmployment> getFreshGraduateList(Long baseMajorId, String year) {
        BaseMajor major = baseMajorRepository.findOne(baseMajorId);
        if (major == null) {
            log.debug("Request to get FreshGraduateEmployment : {}", major);
        }
        return freshGraduateEmploymentRepository.findFreshGraduateEmploymentByBaseMajorAndDataYear(major,year);
    }

    @Override
    public Map getGraduateStuCount(Long baseMajorId, String year) {
        String sql = "SELECT * , COUNT(*) stu_number FROM `fresh_graduate_employment` a, `base_student_info` b WHERE a.base_major_id="+ baseMajorId +" AND a.data_year='"+ year +"' \n" +
            "AND a.base_major_id=b.base_major_id AND a.data_year=b.over_year";
        // String sql = "SELECT COUNT(stu_no) FROM `base_student_info` a WHERE a.`base_major_id` = "+ baseMajorId +" AND a.over_year='"+ overYear +"'";
        return jdbcTemplate.queryForMap(sql);
    }
}
